package com.erlinyou.im.service.group.Impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.erlinyou.base.service.BaseServiceImpl;
import com.erlinyou.im.dao.repository.group.GroupRepository;
import com.erlinyou.im.dao.repository.group.GroupmembersRepository;
import com.erlinyou.im.model.group.Group;
import com.erlinyou.im.model.group.Groupmembers;
import com.erlinyou.im.service.group.GroupService;
import com.erlinyou.travel.model.user.TUser;
@Service
public class GroupServiceImpl extends BaseServiceImpl<Group, Integer> implements GroupService {
	private GroupRepository repository;

	@Resource
	public void setBaseService(GroupRepository repository) {
		this.repository = repository;
		this.baseRepository = repository;
	}
	@Resource
	private GroupmembersRepository groupmembersRepository;
	@Override
	@Transactional
	public boolean createGroup(Group group,List<TUser> imgAndId) throws RuntimeException {
		group = repository.save(group);
		
		if(group!=null) {
			int groupid = group.getId();
			for (TUser tUser : imgAndId) {
				Groupmembers groupmembers = new Groupmembers();
				groupmembers.setCt(group.getCt());
				groupmembers.setGroupid(groupid);
				groupmembers.setUserid(tUser.getUserid());
				groupmembers.setUserName(tUser.getNickName());
				groupmembers.setGtype(1);
				if(groupmembersRepository.save(groupmembers)==null) {
					throw new RuntimeException();
				}
			}
			return true;
		}else {
			throw new RuntimeException();
		}
	}
	@Override
	@Cacheable(value="serviceCache",key="#root.methodName+':'+#userId+':'+#gtype")
	public List<Group> selectBygroup(Long userId,int gtype) {
		return repository.selectBygroup(userId,gtype);
	}

	@Override
	@Transactional
	public boolean inGroup(Group group,List<Groupmembers> groupmembersList) throws RuntimeException {
		if(repository.save(group)!=null){
			if(groupmembersRepository.saveAll(groupmembersList).size() == groupmembersList.size()){
				return true;
			}
		}
		throw new RuntimeException();
	}
	@Override
	@Cacheable(value="serviceCache",key="#root.methodName+':'+#gtype")
	public List<Group> findByGtype(Integer gtype) {
		return repository.findByGtype(gtype);
	}
	
	@Override
	@Transactional
	public boolean saveAndMove(List<Group> grouplist, List<Groupmembers> groupmemberslist) {
		boolean b = repository.saveAll(grouplist)!=null;
		if(b) {
			boolean b1 = groupmembersRepository.saveAll(groupmemberslist)!=null;
			if(b1){
				return true;
			}else {
				throw new RuntimeException();
			}
		}else {
			throw new RuntimeException();
		}
	}
	@Transactional
	@Override
	public boolean saveAddPOI(Group group,Groupmembers groupmembers) {
		Group group1 = repository.save(group);
		if(group1!=null) {
			groupmembers.setGroupid(group1.getId());
			boolean b = groupmembersRepository.save(groupmembers)!=null;
			if(b) {
				return true;
			}else {
				throw new RuntimeException();
			}
		}else {
			throw new RuntimeException();
		}
		
	}
	@Override
	public Group findByPoiId(String poiId) {
		return repository.findByPoiId(poiId);
	}
	@Override
	public List<Group> findCustom(Long userId,Integer code) {
		List<Map<String, Object>> groupmem = groupmembersRepository.findByUserIdAndGtype(userId,code);
		List<Integer> arrayList = new ArrayList<Integer>();
		for (Map<String, Object> map : groupmem) {
			arrayList.add(Integer.parseInt(map.get("groupid").toString()));
		}
		
		List<Group> findAllById = repository.findAllById(arrayList);
		return findAllById;
	}
	@Override
	public Group findByOwnerAndGtype(Long userId, int gtype) {
		return repository.findByOwnerAndGtype(userId,gtype);
	}
	@Override
	public Group findByPoiIdAndGtype(String userId, int gtype) {
		return repository.findByPoiIdAndGtype(userId,gtype);
	}

}
